package com.daniu101.productbargan.service.impl;


import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.daniu101.productbargan.service.IAliyunService;
import org.springframework.stereotype.Service;

@Service
public class AliyunServiceImpl implements IAliyunService {

    @Override
    public boolean sendVerCodeByPhone(String phone, String vercode) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList();
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId("LTAI9Arxoq5xv4Pk")
                // 您的AccessKey Secret
                .setAccessKeySecret("EkwRPvg3TlQwHuVs5EbPAUscGrZlzH");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("副业君")
                .setTemplateCode("SMS_192542265")
                .setTemplateParam("{\"code\":\""+vercode+"\"}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse ssr = client.sendSms(sendSmsRequest);
        String codeRes = ssr.getBody().getCode();
        if ("OK".equals(codeRes)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean sendVerCodeByEmail(String email, String vercode) {
        // 如果是除杭州region外的其它region（如新加坡、澳洲Region），需要将下面的”cn-hangzhou”替换为”ap-southeast-1”、或”ap-southeast-2”。
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI9Arxoq5xv4Pk", "EkwRPvg3TlQwHuVs5EbPAUscGrZlzH");
        // 如果是除杭州region外的其它region（如新加坡region）， 需要做如下处理
        //try {
        //DefaultProfile.addEndpoint(“dm.ap-southeast-1.aliyuncs.com”, “ap-southeast-1”, “Dm”,  “dm.ap-southeast-1.aliyuncs.com”);
        //} catch (ClientException e) {
        //e.printStackTrace();
        //}
        IAcsClient client = new DefaultAcsClient(profile);
        SingleSendMailRequest request = new SingleSendMailRequest();
        try {
            //request.setVersion(“2017-06-22”);// 如果是除杭州region外的其它region（如新加坡region）,必须指定为2017-06-22
            request.setAccountName("vercode@notice2.daniu101.com");
            request.setFromAlias("副业君");
            request.setAddressType(1);
            request.setTagName("daniu101101");
            request.setReplyToAddress(true);
            request.setToAddress(email);
            //可以给多个收件人发送邮件，收件人之间用逗号分开，批量发信建议使用BatchSendMailRequest方式
            //request.setToAddress(“邮箱1,邮箱2”);
            request.setSubject("请查收您的验证码");
            //如果采用byte[].toString的方式的话请确保最终转换成utf-8的格式再放入htmlbody和textbody，若编码不一致则会被当成垃圾邮件。
            //注意：文本邮件的大小限制为3M，过大的文本会导致连接超时或413错误
            request.setHtmlBody("您的验证码是:" + vercode);
            //SDK 采用的是http协议的发信方式, 默认是GET方法，有一定的长度限制。
            //若textBody、htmlBody或content的大小不确定，建议采用POST方式提交，避免出现uri is not valid异常
            request.setMethod(MethodType.POST);
            //开启需要备案，0关闭，1开启
            //request.setClickTrace(“0”);
            //如果调用成功，正常返回httpResponse；如果调用失败则抛出异常，需要在异常中捕获错误异常码；错误异常码请参考对应的API文档;
            client.getAcsResponse(request);

            return true;

        } catch (ServerException e) {
            //捕获错误异常码
            System.out.println("ErrCode :"  + e.getErrCode());
            e.printStackTrace();
        }
        catch (ClientException e) {
            //捕获错误异常码
            System.out.println("ErrCode : " + e.getErrCode());
            e.printStackTrace();
        }

        return false;
    }
}
